package com.ysd.gulimall.product.service.impl;

import com.ysd.gulimall.product.entity.SkuImagesEntity;
import com.ysd.gulimall.product.entity.SpuInfoEntity;
import com.ysd.gulimall.product.service.*;
import com.ysd.gulimall.product.vo.SkuItemSaleAttrVo;
import com.ysd.gulimall.product.vo.SkuItemVo;
import com.ysd.gulimall.product.entity.SpuInfoDescEntity;
import com.ysd.gulimall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.utils.PageUtils;
import com.ysd.common.utils.Query;

import com.ysd.gulimall.product.dao.SkuInfoDao;
import com.ysd.gulimall.product.entity.SkuInfoEntity;

import javax.annotation.Resource;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
    //注入图片业务层
    @Autowired
    private SkuImagesService imagesService;
    //SPU业务层
    @Autowired
    private SpuInfoDescService spuInfoDescService;
    //属性分组业务层
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;

    /**
     * 已创建线程池
     */
    @Resource
    private ThreadPoolExecutor executor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {

        //  key=华为&catelogId=225&brandId=12&min=2&max=7

        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<SkuInfoEntity>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.and(w -> {
                w.eq("sku_id", key).or().like("sku_name", key);
            });
        }

        String brandId = (String) params.get("brandId");
        if (!StringUtils.isEmpty(brandId)) {
            wrapper.eq("brand_id", brandId);
        }
        String catelogId = (String) params.get("catelogId");
        if (!StringUtils.isEmpty(catelogId)) {
            wrapper.eq("catalog_id", catelogId);
        }
        Integer min = Integer.parseInt((String) params.get("min"));
        Integer max = Integer.parseInt((String) params.get("max"));
        if (min != 0 || max != 0) {
            wrapper.between("price", min, max);
        }


        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    /**
     * 查询详情内容
     * 1、获取SKU基本信息
     * 2、获取SKU图片信息
     * 3、获取SKU促销信息
     * 4、获取SPU销售信息
     * 5、获取规格参数组以及组下的规格参数
     * 6、SPU详情
     */
    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();

        CompletableFuture<SkuInfoEntity> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            //1、获取SKU的基本信息  pms_sku_info
            SkuInfoEntity skuInfo = getById(skuId);
            skuItemVo.setInfo(skuInfo);
            return skuInfo;
        }, executor);
        //1 和 2单独线程   345等待1结束，然后平行执行
        //继续下一个线程
        CompletableFuture<Void> saleAttrFuture = skuInfoFuture.thenAcceptAsync((res) -> {
            //3、通过SPU获取旗下的所有销售属性组合
            List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrBySpuId(res.getSpuId());
            skuItemVo.setSaleAttr(saleAttrVos);
        }, executor);


        //继续拼装任务
        CompletableFuture<Void> descFuture = skuInfoFuture.thenAcceptAsync((res) -> {
            //4、获取SPU介绍信息 pms_spu_info_desc    //狗比代码
            SpuInfoDescEntity spuInfoDesc = spuInfoDescService.getDescBySpuId(res.getSpuId());
            skuItemVo.setDesp(spuInfoDesc);
        }, executor);

        //继续拼装任务
        CompletableFuture<Void> baseFuture1 = skuInfoFuture.thenAcceptAsync((res) -> {
            //5、获取规格参数组以及组下的规格参数
            //查询某个SPU的属性分组 以及该属性分组的所有属性及其值
            List<SpuItemAttrGroupVo> groupAttrs
                    = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            skuItemVo.setGroupAttrs(groupAttrs);
        }, executor);

        CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
            //2、获取SKU图片信息  pms_sku_images
            List<SkuImagesEntity> images = imagesService.getImagesBySkuId(skuId);
            skuItemVo.setImages(images);
        }, executor);
        //等待所有任务完成
        CompletableFuture.allOf(saleAttrFuture, descFuture, baseFuture1, imageFuture).get();
        return skuItemVo;
    }

    @Override
    public PageUtils getSkusBySpuId(Map<String, Object> params,Long spuId) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<SkuInfoEntity>();


        wrapper.eq("spu_id", spuId);


        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }


}